<template>
  <div class="activity-wrapper">
    <div class="top d-flex">
      <div class="top-left">
        <el-button
          v-for="(item, index) of buttonList"
          :key="index"
          :class="{ 'active': isActive === index }"
          @click="buttonClick(index)"
        >{{ item.name }}({{ item.num }})</el-button>
      </div>
      <div class="top-right">
        <el-button type="primary" class="add" @click="add">添加</el-button>
      </div>
    </div>
    <div class="bottom">
      <table-list :list="list" @changeSuccess="handleChangeSuccess" />
    </div>
    <Pagination class="pagination" :total="total" :cur-page="currentPage" @nextPage="handleNextPage" />
  </div>
</template>
<script>
import TableList from './table-list'
import { getClusterList, getActiveStatusNum } from '@/api/request/org-group'
import Pagination from '@/common/pagination'
const ERR_OK = 200
export default {
  components: {
    TableList,
    Pagination
  },
  data() {
    return {
      total: 0,
      currentPage: 1,
      list: [],
      buttonList: [{ name: '拼团中', num: 0 }, { name: '待发布', num: 0 }, { name: '已结束', num: 0 }],
      isActive: 0,
      type: 1
    }
  },
  created() {
    this._getClusterList({ page: this.currentPage, type: this.type })
    this._getActiveStatusNum()
  },
  methods: {
    handleChangeSuccess() {
      this._getActiveStatusNum()
    },
    handleNextPage(page) {
      this.currentPage = page
    },
    buttonClick(index) {
      this.isActive = index
      this.currentPage = 1
      switch (this.isActive) {
        case 0:
          this._getClusterList({ page: this.currentPage, type: 1 })
          break
        case 1:
          this._getClusterList({ page: this.currentPage, type: 2 })
          break
        case 2:
          this._getClusterList({ page: this.currentPage, type: 3 })
          break
      }
    },
    add() {
      this.$router.push('/org-group/add-edit')
    },
    _getClusterList({ page, type }) {
      getClusterList({ page, type })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            res.list.forEach((item) => {
              item.is_publish = !!item.is_publish
            })
            this.list = res.list
            this.total = res.total
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _getActiveStatusNum() {
      getActiveStatusNum()
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.buttonList[0].num = res.list.start_num
            this.buttonList[1].num = res.list.wait_num
            this.buttonList[2].num = res.list.end_num
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/styles/el-pagination.scss';
.activity-wrapper {
  box-sizing: border-box;
  background: #fff;
  height: 100%;
  flex: 1;
  padding: 20px;
  box-sizing: border-box;
  .top {
  }
}
.el-button {
  background: #e5e5e5;
  &.active {
    background-color: #5a98de !important;
    color: #fff !important;
  }
}
.add {
  background-color: #5a98de !important;
  color: #fff !important;
}
.d-flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
